package cn.hs.ph.dao.impl;

import cn.hs.ph.dao.GoodsDao;
import cn.hs.ph.entity.Goods;
import cn.hs.ph.util.JDBCDruidUtil;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

public class GoodsDaoImpl implements GoodsDao {
    @Override
    public int insert(Goods goods) {
        return 0;
    }

    @Override
    public int delete(int goodsId) {
        return 0;
    }


    @Override//商品上下架
    public int update(Goods goods) {
        int num=0;

        String sql="update goods set goodsStatus=? where goodsId=?";
        Object[] obj={
                goods.getGoodsStatus(),
                goods.getGoodsId()
        };
        num = JDBCDruidUtil.update(sql, obj);
        return num;
    }

    @Override
    public List<Goods> selectAll(Map<String, String> map) {
        List<Goods> list = new ArrayList<>();
        String sql="select goods.*,shopName from goods,shop " +
                "where goods.shopId=shop.shopId";
        List<Object> params = new ArrayList<>();
        String goodsName = map.get("goodsName");
        //商品名模糊查询
        if(goodsName!=null && !"".equals(goodsName)){
            sql+=" and goodsName like ?";
            params.add("%"+goodsName+"%");
        }
        //所属门店名模糊查询
        String shopName = map.get("shopName");
        if(shopName!=null && !"".equals(shopName)){
            sql+=" and shopName like ?";
            params.add("%"+shopName+"%");
        }
        //时间范围查询
        String startTime = map.get("startTime");
        if (startTime!=null && !"".equals(startTime)){
            String endTime = map.get("endTime");
            sql+=" and addTime between ? and ?";
            params.add(startTime);
            params.add(endTime);
        }
        //状态查询
        String status = map.get("goodsStatus");
        if (status!=null && !"".equals(status)){
            int goodsStatus = Integer.parseInt(status);
            sql+=" and goodsStatus = ?";
            params.add(goodsStatus);
        }
        //分页查询
        String pn = map.get("pageNum");
        if (pn != null && !"".equals(pn)) {
            sql += " limit ?,? ";
            int pageNum = Integer.parseInt(pn);
            int pageSize = Integer.parseInt(map.get("pageSize"));
            params.add((pageNum - 1) * pageSize);
            params.add(pageSize);
        }
        //最终查询
        ResultSet res = JDBCDruidUtil.query(sql, params.toArray());
        while (true) {
            try {
                if (!res.next()) break;
                Goods goods = new Goods(res.getInt(1),
                        res.getString(2),
                        res.getString(3),
                        res.getDouble(4),
                        res.getInt(5),
                        res.getInt(6),
                        res.getInt(7),
                        res.getDate(8),
                        res.getString(9),
                        res.getInt(10),
                        res.getInt(11),
                        res.getString(12));
                list.add(goods);
            } catch (SQLException e) {
                throw new RuntimeException(e);
            }
        }
        JDBCDruidUtil.close();
        return list;
    }

    @Override
    public int selectCount(Map<String, String> map) {
        int count=0;
        String sql="select count(1) from goods,shop " +
                "where goods.shopId=shop.shopId";
        List<Object> params = new ArrayList<>();
        String goodsName = map.get("goodsName");
        //商品名模糊查询
        if(goodsName!=null && !"".equals(goodsName)){
            sql+=" and goodsName like ?";
            params.add("%"+goodsName+"%");
        }
        //所属门店名模糊查询
        String shopName = map.get("shopName");
        if(shopName!=null && !"".equals(shopName)){
            sql+=" and shopName like ?";
            params.add("%"+shopName+"%");
        }
        //时间范围查询
        String startTime = map.get("startTime");
        if (startTime!=null && !"".equals(startTime)){
            String endTime = map.get("endTime");
            sql+=" and addTime between ? and ?";
            params.add(startTime);
            params.add(endTime);
        }
        //状态查询
        String status = map.get("goodsStatus");
        if (status!=null && !"".equals(status)){
            int goodsStatus = Integer.parseInt(status);
            sql+=" and goodsStatus = ?";
            params.add(goodsStatus);
        }
        //分页查询
        String pn = map.get("pageNum");
        if (pn != null && !"".equals(pn)) {
            sql += " limit ?,? ";
            int pageNum = Integer.parseInt(pn);
            int pageSize = Integer.parseInt(map.get("pageSize"));
            params.add((pageNum - 1) * pageSize);
            params.add(pageSize);
        }
        //最终查询
        ResultSet res = JDBCDruidUtil.query(sql, params.toArray());
        while (true) {
            try {
                if (!res.next()) break;
                count=res.getInt(1);
            } catch (SQLException e) {
                throw new RuntimeException(e);
            }

        }
        JDBCDruidUtil.close();
        return count;
    }
}
